package com.shawn.jetpackstady.room;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import com.shawn.jetpackstady.R;
import com.shawn.jetpackstady.room.bean.User;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RoomActivity extends AppCompatActivity {

    UserDao userDao;

    @BindView(R.id.btn_room_select)
    Button btn_room_select;
    @BindView(R.id.btn_room_insert)
    Button btn_room_insert;
    @BindView(R.id.btn_room_delete)
    Button btn_room_delete;
    @BindView(R.id.btn_room_update)
    Button btn_room_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        ButterKnife.bind(this);
        userDao = AppDataBase.getAppDataBase(this).getUserDao();
//        if (savedInstanceState == null) {
//            getSupportFragmentManager().beginTransaction()
//                    .replace(R.id.container, MainFragment.newInstance())
//                    .commitNow();
//        }
    }

    @OnClick(R.id.btn_room_select)
    public void onSelect() {
        // 查询数据
        List<User> userList = AppDataBase.getAppDataBase(this).getUserDao().getAll();
        Log.i("shawn", userList.toString());
    }

    @OnClick(R.id.btn_room_update)
    public void onUpdate(){
        User zhangsan = AppDataBase.getAppDataBase(this).getUserDao().getUserByName("张三");
        zhangsan.setUserID(5);
        zhangsan.setName("张三321");
        userDao.update(zhangsan);
    }

    @OnClick(R.id.btn_room_insert)
    public void onInsert() {
        // 插入数据
        User user = new User();
        user.setSex("男");
        user.setName("张三");
        AppDataBase.getAppDataBase(this).getUserDao().insert(user);
    }

    @OnClick(R.id.btn_room_delete)
    public void onDelete() {
        // 删除对象时 设置的条件 需要是表中的主键 这样才能删除成功
        User user = new User();
        user.setUserID(1);
        AppDataBase.getAppDataBase(this).getUserDao().delete(user);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
